package com.xhtt.modules.app.controller.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.*;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.sys.controller.AbstractController;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.task.entity.TaskShipPlan;
import com.xhtt.modules.task.entity.TaskShipSeo;
import com.xhtt.modules.task.entity.TaskShipSeoLog;
import com.xhtt.modules.task.entity.TaskShipTask;
import com.xhtt.modules.task.model.TaskOperConnectModel;
import com.xhtt.modules.task.model.TaskOperModel;
import com.xhtt.modules.task.model.lmtask.TaskOperLMModel;
import com.xhtt.modules.task.model.oper.TaskOperDCQYModel;
import com.xhtt.modules.task.model.ship.ArrangeModel;
import com.xhtt.modules.task.model.ship.TaskOperCCRModel;
import com.xhtt.modules.task.model.ship.TaskOperOtherModel;
import com.xhtt.modules.task.service.TaskShipPlanService;
import com.xhtt.modules.task.service.TaskShipSeoLogService;
import com.xhtt.modules.task.service.TaskShipTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务平台-装卸船任务
 * </p>
 *
 * @author feipc
 * @since 2019-02-26
 */
@Api(tags = "任务平台-装卸船任务", value = "任务平台-装卸船任务")
@RestController
@RequestMapping("app/task/ship")
public class TaskShipController extends AbstractController {

    @Autowired
    private TaskShipPlanService taskShipPlanService;
    @Autowired
    private TaskShipTaskService taskShipTaskService;
    @Autowired
    private TaskShipSeoLogService taskShipSeoLogService;

    /**
     * 获取船计划列表
     */
    @GetMapping(value = "/planList")
    @ApiOperation("获取船计划列表(web)")
    @Login
    public R planList(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        PageUtils page = taskShipPlanService.planList(params, user);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增船计划
     */
    @PostMapping(value = "/addPlan")
    @ApiOperation("新增船计划(web)")
    @Login
    public R addPlan(@RequestBody TaskShipPlan taskShipPlan) {
        taskShipPlanService.addPlan(taskShipPlan);
        return R.ok();
    }

    /**
     * 外部调用--新增船计划
     */
    @PostMapping(value = "/request_ship_info")
    @ApiOperation("外部调用--新增船计划")
    public R addPlanFromOut(@RequestBody TaskShipPlan taskShipPlan) {
        taskShipPlanService.addPlanFromOut(taskShipPlan);
        return R.ok();
    }

    /**
     * 删除船计划
     */
    @PostMapping(value = "/deletePlan")
    @ApiOperation("删除船计划(web)")
    @Login
    public R deletePlan(@RequestBody Integer[] ids) {
        taskShipPlanService.deletePlan(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改船计划
     */
    @PostMapping(value = "/updatePlan")
    @ApiOperation("修改船计划(web)")
    @Login
    public R updatePlan(@RequestBody TaskShipPlan taskShipPlan) {
        taskShipPlanService.updatePlan(taskShipPlan);
        return R.ok();
    }

    /**
     * 获取船计划详情
     */
    @GetMapping(value = "/getPlan/{id}")
    @ApiOperation("获取船计划详情")
    @Login
    public R getPlan(@PathVariable("id") Integer id) {
        TaskShipPlan taskShipPlan = taskShipPlanService.getPlan(id);
        return R.ok().put(Constant.DATA, taskShipPlan);
    }

    /**
     * 任务安排弹出页面的展示内容
     */
    @GetMapping(value = "/getArrangeInfo")
    @ApiOperation("获取-任务安排弹出页面的展示内容(web)")
    @Login
    public R getArrangeInfo(@RequestParam Integer id) {
        ArrangeModel arrangeModel = taskShipPlanService.getArrangeInfo(id);
        return R.ok().put(Constant.DATA, arrangeModel);
    }

    /**
     * 任务安排提交
     */
    @PostMapping(value = "/arrange")
    @ApiOperation("任务安排提交（web）")
    @Login
    public R arrange(@RequestBody ArrangeModel arrangeModel) {
        if (null == arrangeModel.getPlanId()) {
            throw new RRException("参数有误");
        }
        taskShipPlanService.arrange(arrangeModel);
        return R.ok();
    }

    /**
     * 任务分配-退回
     */
    @PostMapping(value = "/back")
    @ApiOperation("任务分配-退回（web）")
    @Login
    public R back(@RequestBody TaskShipPlan plan) {
        taskShipPlanService.back(plan);
        return R.ok();
    }

//    /**
//     * 指派任务页面-查看排管任务的详情
//     */
//    @GetMapping(value = "/getConnectTaskDetail")
//    @ApiOperation("指派任务页面-查看排管任务的详情(web)")
//    @Login
//    public R getConnectTaskDetail(@RequestParam Integer id) {
//        List<TaskShipTaskProdStep> list = taskProdStepService.getConnectTaskDetail(id);
//        return R.ok().put(Constant.DATA, list);
//    }

    /**
     * 指派任务页面-查看任务
     */
    @GetMapping(value = "/getTaskDetail")
    @ApiOperation("指派任务页面-查看任务(web)")
    @Login
    public R getTaskDetail(@RequestParam Integer id, @ApiIgnore @LoginUser SysUserEntity user) {
        ArrangeModel am = taskShipTaskService.getTaskDetail(id, user);
        return R.ok().put(Constant.DATA, am);
    }

    /**
     * 指派任务
     */
    @PostMapping(value = "/pubTask")
    @ApiOperation("指派任务(web)")
    @Login
    public R pubTask(@RequestBody List<TaskShipTask> tasks, @ApiIgnore @LoginUser SysUserEntity user) {
        //其实是更新字段操作，所以判断一下id、
        if (null == tasks.get(0).getId()) {
            throw new RRException("参数有误");
        }

        taskShipTaskService.pubTask(tasks, user);
        return R.ok();
    }

//    /**
//     * 查看任务的产品（LM任务用的）
//     */
//    @GetMapping(value = "/getTaskProduct")
//    @ApiOperation("查看任务的产品（LM任务用的）(app)")
//    @Login
//    public R getTaskProduct(@RequestParam Integer taskId) {
//        List<String> products = taskShipTaskService.getTaskProduct(taskId);
//        return R.ok().put(Constant.DATA, products);
//    }

    /**
     * 任务确认-查看任务作业详情
     */
    @GetMapping(value = "/taskOperDetail")
    @ApiOperation("任务确认-查看任务作业详情(web/app)")
    @Login
    public R taskOperDetail(@RequestParam Integer taskId, @ApiIgnore @LoginUser SysUserEntity user) {
        TaskOperModel model = taskShipTaskService.taskOperDetail(taskId, user.getUserId().intValue());
        return R.ok().put(Constant.DATA, model);
    }

    /**
     * 查看CCR任务作业详情(多个)
     */
    @GetMapping(value = "/taskCCRDetail")
    @ApiOperation("查看CCR任务作业详情(多个)(web/app)")
    @Login
    public R taskCCRDetail(@RequestParam Integer taskId, @ApiIgnore @LoginUser SysUserEntity user) {
        List<TaskOperCCRModel> modelList = taskShipTaskService.taskCCRDetail(taskId, user.getUserId().intValue());
        return R.ok().put(Constant.DATA, modelList);
    }

    /**
     * 查看CCR任务作业状态
     */
    @GetMapping(value = "/taskCCRStatus")
    @ApiOperation("查看CCR任务作业状态(app)")
    @Login
    public R taskCCRStatus(@RequestParam Integer taskId) {
        String state = taskShipTaskService.taskCCRStatus(taskId);
        return R.ok().put(Constant.DATA, state);
    }

    /**
     * APP点击开始操作
     */
    @PostMapping(value = "/start")
    @ApiOperation("APP点击开始操作(app)")
    @Login
    public R start(Integer taskId) {
        taskShipTaskService.start(taskId);
        return R.ok();
    }


    /**
     * APP临时保存管线任务
     */
    @PostMapping(value = "/tempConnectSave")
    @ApiOperation("APP临时保存管线任务(app)")
    @Login
    public R tempConnectSave(@RequestBody TaskOperConnectModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.tempSave(model);
        return R.ok();
    }

    /**
     * APP提交管线任务
     */
    @PostMapping(value = "/submitConnectTask")
    @ApiOperation("APP提交管线任务(app)")
    @Login
    public R submitConnectTask(@RequestBody TaskOperConnectModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.submitTask(model);
        return R.ok();
    }

    /**
     * APP临时保存LM任务
     */
    @PostMapping(value = "/tempLMSave")
    @ApiOperation("APP临时保存LM任务(app)")
    @Login
    public R tempLMSave(@RequestBody TaskOperLMModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.tempSave(model);
        return R.ok();
    }

    /**
     * APP提交LM任务
     */
    @PostMapping(value = "/submitLMTask")
    @ApiOperation("APP提交LM任务(app)")
    @Login
    public R submitLMTask(@RequestBody TaskOperLMModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.submitTask(model);
        return R.ok();
    }

    /**
     * APP临时保存CCR任务
     */
    @PostMapping(value = "/tempCCRSave")
    @ApiOperation("APP临时保存CCR任务(app)")
    @Login
    public R tempCCRSave(@RequestBody TaskOperCCRModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.tempSave(model);
        return R.ok();
    }

    /**
     * APP提交CCR任务
     */
    @PostMapping(value = "/submitCCRTask")
    @ApiOperation("APP提交CCR任务(app)")
    @Login
    public R submitCCRTask(@RequestBody TaskOperCCRModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.submitTask(model);
        return R.ok();
    }

    /**
     * APP临时保存打尺取样任务
     */
    @PostMapping(value = "/tempDCQYSave")
    @ApiOperation("APP临时保存打尺取样任务(app)")
    @Login
    public R tempDCQYSave(@RequestBody TaskOperDCQYModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.tempSave(model);//这边共用oper那边的打尺取样任务model
        return R.ok();
    }

    /**
     * APP提交打尺取样任务
     */
    @PostMapping(value = "/submitDCQYTask")
    @ApiOperation("APP提交打尺取样任务(app)")
    @Login
    public R submitDCQYTask(@RequestBody TaskOperDCQYModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.submitTask(model);//这边共用oper那边的打尺取样任务model
        return R.ok();
    }

    /**
     * APP临时保存其他任务
     */
    @PostMapping(value = "/tempOtherSave")
    @ApiOperation("APP临时保存其他任务(app)")
    @Login
    public R tempOtherSave(@RequestBody TaskOperOtherModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.tempSave(model);
        return R.ok();
    }

    /**
     * APP提交其他任务
     */
    @PostMapping(value = "/submitOtherTask")
    @ApiOperation("APP提交其他任务(app)")
    @Login
    public R submitOtherTask(@RequestBody TaskOperOtherModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskShipTaskService.submitTask(model);
        return R.ok();
    }


    /**
     * 任务确认-任务列表
     */
    @GetMapping(value = "/confirmTaskList")
    @ApiOperation("任务确认-任务列表(web)")
    @Login
    public R confirmTaskList(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        params.put("confirmUserId", user.getUserId());
        PageUtils page = taskShipTaskService.confirmTaskList(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 任务确认-任务确认的提交
     */
    @PostMapping(value = "/commitTask")
    @ApiOperation("任务确认的提交(web/app)")
    @Login
    public R commitTask(@RequestBody TaskOperModel model, @ApiIgnore @LoginUser SysUserEntity user) {
        taskShipTaskService.commitTask(model, user.getUserId().intValue());
        return R.ok();
    }

    /**
     * 船舶一览表
     */
    @GetMapping(value = "/core/seo")
    @ApiOperation("船舶一览表(web)")
    @Login
    public R seoList(@RequestParam Map<String, Object> params) {
        PageUtils page = taskShipPlanService.seoList(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 船舶一览表编辑
     */
    @PostMapping(value = "/core/seoEdit")
    @ApiOperation("船舶一览表编辑(web)")
    @Login
    public R seoEdit(@RequestBody TaskShipSeo seo, @ApiIgnore @LoginUser SysUserEntity user) {
        taskShipPlanService.seoEdit(seo, user);
        return R.ok();
    }

    /**
     * 船舶一览表日志
     */
    @GetMapping(value = "/core/seoLog")
    @ApiOperation("船舶一览表日志(web)")
    @Login
    public R seoLog(@RequestParam("id") Integer sppId) {
        List<TaskShipSeoLog> list = taskShipSeoLogService.list(new LambdaQueryWrapper<TaskShipSeoLog>().eq(TaskShipSeoLog::getSppId, sppId));
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 船舶一览表-导出
     */
    @GetMapping(value = "/core/seoExport")
    @ApiOperation("船舶一览表-导出(web)")
    @Login
    public R seoListExport(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        params.put("limit", "65000");
        ExportExcel exportExcel = new ExportExcel();
        PageUtils page = taskShipPlanService.seoList(params);
        List list = page.getList();

        String fileName = "船舶任务";
        String sheetName = "船舶任务";
        String[] headers = {"日期", "船名", "装/卸", "产品", "罐号", "码头", "班组", "码头作业（人员）", "码头作业", "软管连接", "通球", "前尺", "CCR记录",
                "预计靠泊时间", "实际靠泊时间", "前尺开始时间", "前尺结束时间", "岸方开始接管时间", "岸方结束接管时间", "商检上船时间", "商检计量结束时间", "L/M上船时间",
                "安全会议结束时间", "起泵时间", "停泵时间", "商检验空舱时间", "拆除软管时间", "离泊时间", "开始通球时间", "结束通球时间", "状态"};
        String[] columns = {"date", "shipName", "planTypeName", "productName", "potName", "berthName", "teamName", "lmUser", "lmStatusName", "hoseStatusName", "ballStatusName", "dcqyStatusName", "ccrStatusName"
                , "planKb", "kb", "dcqyStartTime", "dcqyFinishTime", "hoseStartTime", "hoseFinishTime", "dlsc", "jlwc", "lmdl"
                , "aqhy", "qb", "tb", "sjddyc", "cg", "lb", "ballStartTime", "ballFinishTime", "planStatusName"};
        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, list, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
        return R.ok().put(Constant.DATA, page);
    }
}
